import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class test {

    public static void main(String[] args) throws IOException {
        Logger log = LogManager.getLogger(test.class);
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url ="jdbc:sqlserver://192.168.1.21;database=CDRDB1;useUnicode=true&characterEncoding=UTF-8";
            Connection con = DriverManager.getConnection(url,"sa","yishidb");
            System.out.println("数据库连接成功");
            Connection conn=null;
            PreparedStatement pstmt=null;
            ResultSet rs=null;
            String sql="SELECT TOP 10000 [COM_MAPPING_ID]\n" +
                    "      ,[COM_TYPE_ID]\n" +
                    "      ,[COM_TYPE_NAME]\n" +
                    "      ,[SOURCE_CODE]\n" +
                    "      ,[SOURCE_NAME]\n" +
                    "      ,[TARGET_CODE]\n" +
                    "      ,[TARGET_NAME]\n" +
                    "  FROM [CDRDB1].[dbo].[DC_DICT_COMMON_MAPPING] order by [COM_MAPPING_ID] desc";
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while(rs.next()){
                int  COM_MAPPING_ID= rs.getInt("COM_MAPPING_ID");
                String COM_TYPE_ID=rs.getString("COM_TYPE_ID");
                String COM_TYPE_NAME=rs.getString("COM_TYPE_NAME");
                String SOURCE_CODE= rs.getString( "SOURCE_CODE");
                String SOURCE_NAME=rs.getString("SOURCE_NAME");
                String TARGET_CODE= rs.getString( "TARGET_CODE");
                String TARGET_NAME= rs.getString( "TARGET_NAME");
                log.info(COM_MAPPING_ID+","+COM_TYPE_ID+","+COM_TYPE_NAME+","+SOURCE_CODE+","+SOURCE_NAME+","+TARGET_CODE+","+TARGET_NAME);
               Thread.sleep(2000);
            }
            con.close();
        }
        catch(Exception e) {
            System.out.println("数据库连接失败\n" + e.toString());
        }
    }
}
